home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / window.cq / window.c
Encoding:
C/C++ Source or Header  |  1985-12-01  |  6.4 KB  |  325 lines

  1. #include "stdio.h"
  2.  
  3. typedef struct wcb    /* --- window control block    ---        */
  4. {
  5.     int        ulx,    /* upper left corner x coordinate    */
  6.             uly,    /* ditto y coordinate                */
  7.             xsize,    /* line width of inside of window    */
  8.             ysize,    /* number of lines inside of window    */
  9.             cx,        /* current cursor offset in window    */
  10.             cy,
  11.             style,    /* attribute to be used in window    */
  12.             *scrnsave,    /* pointer to screen save buffer */
  13.             oldx,    /* cursor position when window was    */
  14.             oldy;    /* opened (used for screen restore)    */
  15. } WINDOW, *WINDOWPTR;
  16.  
  17. putch(x, y, c,attr)
  18. int        x, y, c,attr;
  19. {
  20.     gotoxy(x, y, 0);
  21.     vputca(c,0x01, 0, 1);
  22. }
  23.  
  24.  
  25. getch(x, y)
  26. int        x, y;
  27. {
  28.     gotoxy(x, y, 0);
  29.     return(vgetc(0));
  30. }
  31.  
  32.  
  33. draw_row(x, y, count, c)
  34. int        x, y, count,c;
  35. {
  36.     gotoxy(x, y, 0);
  37.     vputca(c,0x07, 0, count);
  38. }
  39.  
  40.  
  41. draw_window(x, y, width, height, attrib)
  42. int        x, y, width, height, attrib;
  43. {
  44.     WINDOWPTR wn;
  45.     int        tx, ty,
  46.             xend, yend;
  47.     int        *tptr;
  48.     char    *calloc();
  49.  
  50.     if ((wn = (WINDOWPTR)calloc(1, sizeof(WINDOW))) == NULL)
  51.         return(NULL);
  52.     else if ((wn->scrnsave = (int *)calloc((width+2) * (height+2), sizeof(int))) == NULL)
  53.     {
  54.         free(wn);
  55.         return(NULL);
  56.     }
  57.     else
  58.     {
  59.         /* store parameters in window control block */
  60.  
  61.         wn->ulx = x;
  62.         wn->uly = y;
  63.         wn->xsize = width;
  64.         wn->ysize = height;
  65.         wn->cx = 1;
  66.         wn->cy = 1;
  67.         wn->style = attrib;
  68.         attrib <<= 8;    /* will make things below go quicker */
  69.         wn->oldx = getxy(0) & 255;
  70.         wn->oldy = getxy(0) >> 8;
  71.  
  72.         tptr = wn->scrnsave;
  73.         xend = x + width + 2;
  74.         yend = y + height + 2;
  75.  
  76.         for (ty = y; ty < yend; ty++)
  77.         {
  78.             for (tx = x; tx < xend; tx++)
  79.                 *tptr++ = getch(tx, ty);
  80.         }
  81.  
  82.         putch(x, y, 0xda + attrib,attrib);                    /* ul corner */
  83.         draw_row(x + 1, y, width, 0xc4 + attrib);    /* horiz bar */
  84.         putch(x + width + 1, y, 0xbf + attrib,attrib);        /* ur corner */
  85.  
  86.         yend = y + height;
  87.  
  88.         for (ty = y+1; ty <= yend; ty++)
  89.         {
  90.             putch(x, ty, 0xb3 + attrib,attrib);            /* draw the sides */
  91.             putch(x+width+1, ty, 0xb3 + attrib,attrib);
  92.         }
  93.  
  94.         putch(x, y + height + 1, 0xc0 + attrib,attrib);                /* ll corner */
  95.         draw_row(x + 1, y + height + 1, width, 0xc4 + attrib);    /* horiz bar */
  96.         putch(x + width + 1, y + height + 1, 0xd9 + attrib,attrib);    /* lr corner */
  97.  
  98.         clr_window(wn);
  99.  
  100.         return(wn);
  101.     }
  102. }
  103.  
  104.  
  105. remove_window(wn)
  106. WINDOWPTR wn;
  107. {
  108.     int        tx, ty,
  109.             xend, yend;
  110.     int        *tptr;
  111.  
  112.     tptr = wn->scrnsave;
  113.     xend = wn->ulx + wn->xsize + 2;
  114.     yend = wn->uly + wn->ysize + 2;
  115.  
  116.     for (ty = wn->uly; ty < yend; ty++)
  117.     {
  118.         for (tx = wn->ulx; tx < xend; tx++)
  119.             putch(tx, ty, *tptr++,0x01);
  120.     }
  121.  
  122.     gotoxy(wn->oldx, wn->oldy, 0);
  123.  
  124.     free(wn->scrnsave);
  125.     free(wn);
  126. }
  127.  
  128.  
  129. write_text(wn, attr, string)
  130. WINDOWPTR wn;
  131. char    *string;
  132. int attr;
  133. {
  134.     int        tx, ty, xend;
  135.  
  136.     if (wn->cy > wn->ysize)
  137.     {
  138.         delete_row(wn, 1);
  139.         --wn->cy;
  140.     }
  141.  
  142.     if (wn->cx > 0)
  143.         tx = wn->ulx + wn->cx;
  144.     else
  145.     {
  146.         if (-wn->cx < strlen(string))
  147.             string -= wn->cx;
  148.         else
  149.             *string = '\0';
  150.         tx = wn->ulx + 1;
  151.     }
  152.     xend = wn->ulx + wn->xsize + 1;
  153.     ty = wn->uly + wn->cy;
  154.     while ((tx < xend) && *string)
  155.     {
  156.         gotoxy(tx++, ty, 0);
  157.         vputca(*string++,attr, 0, 1);
  158.     }
  159.     ++wn->cy;        /* move the internal cursor to the next line */
  160. }
  161.  
  162.  
  163. insert_row(wn, row)
  164. WINDOWPTR wn;
  165. int        row;
  166. {
  167.     int        scrlwn[4];
  168.  
  169.     /* calculate corners of the scrolling window */
  170.  
  171.     scrlwn[0] = wn->ulx + 1;            /* ulx */
  172.     scrlwn[1] = wn->uly + row;            /* uly */
  173.     scrlwn[2] = wn->ulx + wn->xsize;    /* lrx */
  174.     scrlwn[3] = wn->uly + wn->ysize;    /* lry */
  175.  
  176.     scrldn(scrlwn, 1, wn->style);
  177. }
  178.  
  179.  
  180. delete_row(wn, row)
  181. WINDOWPTR wn;
  182. int        row;
  183. {
  184.     int        scrlwn[4];
  185.  
  186.     /* calculate corners of the scrolling window */
  187.  
  188.     scrlwn[0] = wn->ulx + 1;            /* ulx */
  189.     scrlwn[1] = wn->uly + row;            /* uly */
  190.     scrlwn[2] = wn->ulx + wn->xsize;    /* lrx */
  191.     scrlwn[3] = wn->uly + wn->ysize;    /* lry */
  192.  
  193.     scrlup(scrlwn, 1, wn->style);
  194. }
  195.  
  196.  
  197. clr_window(wn)
  198. WINDOWPTR wn;
  199. {
  200.     int        scrlwn[4];
  201.  
  202.     /* calculate corners of the scrolling window */
  203.  
  204.     scrlwn[0] = wn->ulx + 1;            /* ulx */
  205.     scrlwn[1] = wn->uly + 1;            /* uly */
  206.     scrlwn[2] = wn->ulx + wn->xsize;    /* lrx */
  207.     scrlwn[3] = wn->uly + wn->ysize;    /* lry */
  208.  
  209.     scrlup(scrlwn, 0, wn->style);
  210.     wn->cx = 1;
  211.     wn->cy = 1;
  212. }
  213.  
  214.  
  215. gotoxy(x, y, page)
  216. int x, y, page;
  217. {
  218. unsigned inr[4], outr[4];
  219.  
  220.       inr[3] =  y;
  221.       inr[3] <<= 8;
  222.       inr[3] |= x;
  223.       inr[1] = page;
  224.       inr[1] <<= 8;
  225.       inr[0] = 0x200;
  226.       sysint(0x10, &inr, &outr);
  227.  
  228. }
  229.  
  230. vputca(chr,attr, page, count)
  231. int chr,page, count,attr;
  232. {
  233. unsigned inr[4], outr[4];
  234.  
  235.              inr[1] = page;     /* bh */
  236.              inr[1] <<= 8;
  237.              inr[1] |= attr;    /* bl */
  238.              inr[2] = count;  /* cx */
  239.              inr[0] = 9;    /* ah */
  240.              inr[0] <<= 8;
  241.              inr[0] |= chr & 0xff;    /* al */
  242.              sysint(0x10, &inr, &outr);
  243. }
  244.  
  245. vgetc(page)
  246. int page;
  247. {
  248. unsigned inr[4], outr[4];
  249.  
  250.       inr[1] = page;
  251.       inr[1] <<= 8;
  252.       inr[0] = 0x800;
  253.       sysint(0x10, &inr, &outr);
  254.       return(outr[0]);
  255.  
  256. }
  257.  
  258. getxy(page)
  259. int page;
  260. {
  261. unsigned inr[4], outr[4];
  262.  
  263.       inr[1] = page;
  264.       inr[1] <<= 8;
  265.       inr[0] = 0x300;
  266.       sysint(0x10, &inr, &outr);
  267.       return(outr[3]);
  268.  
  269. }
  270.  
  271. vputc(chr, page, count)
  272. int chr, page, count;
  273. {
  274. unsigned inr[4], outr[4];
  275. char attr='7';
  276.              inr[1] = page;     /* bh */
  277.              inr[1] <<= 8;
  278.              inr[1] |= attr & 0xff;
  279.              inr[2] = count;  /* cx */
  280.              inr[0] = 10;    /* ah */
  281.              inr[0] <<= 8;
  282.              inr[0] |= chr & 0xff;    /* al */
  283.              sysint(0x10, &inr, &outr);
  284.  
  285. }
  286.  
  287. scrlup(window, count, attrib)
  288. int window[4], count, attrib;
  289. {
  290. unsigned inr[4], outr[4];
  291.  
  292.       inr[1] = attrib;
  293.       inr[1] <<= 8;
  294.       inr[0] = 0x600;
  295.       inr[0] |= count;
  296.       inr[2] = window[1];
  297.       inr[2] <<= 8;
  298.       inr[2] |= window[0];
  299.       inr[3] = window[3];
  300.       inr[3] <<= 8;
  301.       inr[3] |= window[2];
  302.       sysint(0x10, &inr, &outr);
  303.  
  304. }
  305.  
  306. scrldn(window, count, attrib)
  307. int window[4], count, attrib;
  308. {
  309. unsigned inr[4], outr[4];
  310.  
  311.       inr[1] = attrib;
  312.       inr[1] <<= 8;
  313.       inr[0] = 0x700;
  314.       inr[0] |= count;
  315.       inr[2] = window[1];
  316.       inr[2] <<= 8;
  317.       inr[2] |= window[0];
  318.       inr[3] = window[3];
  319.       inr[3] <<= 8;
  320.       inr[3] |= window[2];
  321.       sysint(0x10, &inr, &outr);
  322.  
  323. }
  324.  
  325.